package com.example.springboot.service.impl;

import com.example.springboot.common.PageBean.PageBean;
import com.example.springboot.entity.User;
import com.example.springboot.mapper.UserMapper;
import com.example.springboot.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public PageBean page(int pageNum, int pageSize, String name) {
        //设置分页参数
        PageHelper.startPage(pageNum,pageSize);
        //执行查询
        List<User> list = userMapper.list(name);
        Page p = (Page) list;
        PageBean pageBean =new PageBean(p.getTotal(),p.getResult(), (long) pageSize, (long) pageNum);
        return pageBean;
    }

    @Override
    public void add(User user) {
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setPassword(DigestUtils.md5Hex(user.getPassword()));
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.update(user);
    }

    @Override
    public void delete(Long id) {
        userMapper.deleteById(id);
    }

    @Override
    public void deleteAll(List<Long> ids) {
        userMapper.delete(ids);
    }

    @Override
    public User getById(Long userId) {
        User user = userMapper.getById(userId);
        return user;
    }

    @Override
    public List<String> getByRoleName(String roleName) {
        List<String> name = userMapper.getByRoleName(roleName);
        return name;
    }

    @Override
    public List<User> list() {
        return userMapper.getAll();
    }

}
